{{- $name := .Chart.Name }}
{{- range $index, $val := .Values.items }}
{{- if eq $val.type "ingress" }}
{{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
apiVersion: networking.k8s.io/v1
{{- else }}
apiVersion: networking.k8s.io/v1beta1
{{- end }}
kind: Ingress
metadata:
  {{- if $val.type }}
  name: {{ $val.name | default (print $name "-" $val.type "-" $index) }}
  {{- else }}
  name: {{ $val.name | default (print $name "-ingress-" $index) }}
  {{- end }}
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name "-ingress") }}
{{- if $val.annotations }}
  annotations:
{{- toYaml $val.annotations | nindent 4 }}
{{- else }}
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/proxy-body-size: 50m
{{- end }}
spec:
  {{- if $val.tls }}
  {{- if $val.tls.enabled }}
  tls:
    {{- range $val.rules }}
    - hosts:
      - {{ .host | quote }}
    {{- end }}
      secretName: {{ $val.tls.name | default (print $name "-secret-tls") }}
  {{- end }}{{- end }}
  rules:
    {{- range $val.rules }}
    - host: {{ .host | quote }}
      http:
        paths:
          {{- range .http.paths }}
          - path: {{ .path | default "/" }}
            {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }}
            pathType: {{ .pathType | default (print "ImplementationSpecific") }}
            {{- end }}
            backend:
              {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
              service:
                name: {{ .backend.serviceName | default (print $name "-service" )}}
                port:
                  number: {{ .backend.servicePort }}
              {{- else }}
              serviceName: {{ .backend.serviceName | default (print $name "-service") }}
              servicePort: {{ .backend.servicePort | default (print "3000") }}
              {{- end }}
          {{- end }}
  {{- end }}
{{ print "---" }}
{{- end }}{{- end }}
{{- range $index, $val := .Values.items }}
{{- if not (empty $val.containers) }}
{{- range $Index, $Containers := $val.containers }}
{{- if and $Containers.url (semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion) }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ $val.name | default (print $name "-ingress-" $index "-" $Index) }}
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name "-ingress") }}
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/proxy-body-size: 50m
spec:
  {{- if $val.tls }}
  {{- if $val.tls.enabled }}
  tls:
    - hosts:
        - {{ $Containers.url | quote }}
      secretName: {{ $val.tls.name | default (print $name "-secret-tls") }}
  {{- end }}{{- end }}
  rules:
    {{- range $indexPorts, $Ports := $Containers.ports }}
    - host: {{ $Containers.url | quote }}
      http:
        paths:
        - backend:
            {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }}
            service:
              name: {{ $val.name | default (print $name "-service-" $index "-" $Index) }}
              port:
                number: {{ printf "3%v%v%v" $index $Index $indexPorts }}
            {{- else }}
            serviceName: {{ $val.name | default (print $name "-service-" $index "-" $Index) }}
            servicePort: {{ printf "3%v%v%v" $index $Index $indexPorts }}
            {{- end }}
          path: {{ $Ports.path | default (print "/") }}
          {{- if semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion }}
          pathType: {{ $Ports.pathType | default (print "ImplementationSpecific") }}
          {{- end }}
    {{- end }}
{{ print "---" }}
{{- end }}{{- end }}{{- end }}{{- end }}
